<template>
  <div>
    <el-form :model="form" label-width="120px">
      <h3>图书借阅系统</h3>
      <el-form-item label="用户名" prop="name">
        <el-input v-model="form.username" placeholder="请输入你的用户名"/>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
            v-model="form.password"
            type="password"
            placeholder="请输入密码"
            show-password
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="login">
          登录
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script setup>
import {reactive, ref, watch} from 'vue'
import {useRouter,useRoute} from 'vue-router'
import {useGetTokenStore} from "@/stores";

const userToken = useGetTokenStore()
const router = useRouter()
const route = useRoute()
const form = reactive({
  username: '',
  password: ''
})
const redirect = ref(undefined)
watch(
    () => route.query.redirect,
    (toPath) => {
      redirect.value = toPath
    },
    {
      immediate: true
    }
)

function login() {
  userToken.login(form).then(() => {
    console.log(redirect.value)
    router.push({path: redirect.value || '/'})
  }).catch(() => {

  })
}

</script>
